#include "mytcpsocket.h"
#include "noticeitem.h"
#include "ui_noticeitem.h"

NoticeItem::NoticeItem(struct st_notice notice,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NoticeItem)
{
    ui->setupUi(this);

    m_notice = notice;

    switch(m_notice.type){
    case ADD_FRIEND_REQUEST:{
        QString hname = QString("%1.png").arg(m_notice.fdata.dd_num);
        QString head_url = QString("%1/%2").arg(PATH_IMG).arg(hname);
        QPixmap pixmap = PixmapToRoundOpenCV(downLoadFromUrl(head_url));

        ui->le_whatdo->setText("请求添加你为好友");
        ui->stackedWidget->setCurrentWidget(ui->page_addfriend);
        ui->le_head->setPixmap(pixmap);
        ui->le_nick->setText(m_notice.fdata.nick);
        }
        break;
    case ADD_FRIEND_RESULT:{

        QString hname = QString("%1.png").arg(m_notice.fdata.dd_num);
        QString head_url = QString("%1/%2").arg(PATH_IMG).arg(hname);
        QPixmap pixmap = PixmapToRoundOpenCV(downLoadFromUrl(head_url));

        if(m_notice.result == 0)
            ui->le_whatdo_2->setText("拒绝添加你为好友");
        else
            ui->le_whatdo_2->setText("同意添加你为好友");
        ui->stackedWidget->setCurrentWidget(ui->page_result);
        ui->le_head_2->setPixmap(pixmap);
        ui->le_nick_2->setText(m_notice.fdata.nick);
    }
        break;
    case DELETE_FRIEND_NOTICE:{
        QString hname = QString("%1.png").arg(m_notice.fdata.dd_num);
        QString head_url = QString("%1/%2").arg(PATH_IMG).arg(hname);
        QPixmap pixmap = PixmapToRoundOpenCV(downLoadFromUrl(head_url));


        ui->le_whatdo_2->setText("解除了与你的好友关系");
        ui->stackedWidget->setCurrentWidget(ui->page_result);
        ui->le_head_2->setPixmap(pixmap);
        ui->le_nick_2->setText(m_notice.fdata.nick);
    }
        break;
    case DISSOLVE_GROUP_NOTICE:{

        ui->stackedWidget->setCurrentWidget(ui->page_result);
        ui->le_whatdo_2->setText(QString("群聊[%1]已被解散").arg(m_notice.gdata.gname));
        ui->le_head_2->setPixmap(QPixmap(":/prefix2/software/sysnotice.png"));
        ui->le_nick_2->setText("[系统消息]");
    }
        break;
    case WELCOME:{
        ui->stackedWidget->setCurrentWidget(ui->page_result);
        ui->le_whatdo_2->setText("欢迎使用龙通讯!");
        ui->le_whatdo_2->setWordWrap(true);
        ui->le_head_2->setPixmap(QPixmap(":/prefix2/software/sysnotice.png"));
        ui->le_nick_2->setText("[系统消息]");
    }
        break;
    case NEW_GROUP_NOTICE:{
        ui->stackedWidget->setCurrentWidget(ui->page_newgroup);

        QString gname = QString("%1.png").arg(m_notice.gdata.gid);
        QString head_url = QString("%1/%2").arg(PATH_IMG).arg(gname);
        QPixmap pixmap = PixmapToRoundOpenCV(downLoadFromUrl(head_url));

        ui->le_whatdo_4->setText("好友创建了群聊");
        ui->le_head_4->setPixmap(pixmap);
        ui->le_nick_4->setText(notice.gdata.gname);
    }
        break;
    }

    setQssStyle(QString("../style/NoticeItem.qss"),this);
}

NoticeItem::~NoticeItem()
{
    delete ui;
}

void NoticeItem::on_btn_agree_clicked()
{
    QVariant var;
    var.setValue(m_notice);
    emit agreeOrRefuse(1,var);
}


void NoticeItem::on_btn_refuse_clicked()
{
    QVariant var;
    var.setValue(m_notice);
    emit agreeOrRefuse(0,var);
}


void NoticeItem::on_btn_delete_clicked()
{
    QVariant var;
    var.setValue(m_notice);
    emit agreeOrRefuse(2,var);
}


void NoticeItem::on_btn_add_clicked()
{
    //获取自己的uid
    int uid = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
    //封装添加群聊请求
    QVariantMap map;
    map["cmd"]="add_group";
    map["uid"]=uid;
    map["gid"]=m_notice.gdata.gid;

    //发送请求
    GlobalObject::getGlobalObject()->sendRequest(map);

    // 加入后删除这个通知
    QVariant var;
    var.setValue(m_notice);
    emit agreeOrRefuse(2,var);
}

